package com.bzkj.web.controller.storage;

import com.bzkj.basicinfo.domain.warehouse.PdaWarehouseOutBillsList;
import com.bzkj.basicinfo.service.warehouse.IPdaWarehouseOutBillsService;
import com.bzkj.common.annotation.Log;
import com.bzkj.common.core.controller.BaseController;
import com.bzkj.common.core.domain.AjaxResult;
import com.bzkj.common.core.page.TableDataInfo;
import com.bzkj.common.enums.BusinessType;
import com.bzkj.common.utils.SecurityUtils;
import com.bzkj.common.utils.poi.ExcelUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 出库单据管理Controller
 * 
 * @author system
 * @date 2025-10-17
 */
@RestController
@RequestMapping("/storage/finishStore")
public class FinishStoreController extends BaseController {
    
    @Resource
    private IPdaWarehouseOutBillsService pdaWarehouseOutBillsService;

    /**
     * 查询出库单据列表
     */
    //@PreAuthorize("@ss.hasPermi('storage:finishStore:list')")
    @GetMapping("/list")
    public TableDataInfo list(@RequestParam(required = false) String orderStatus,
                              @RequestParam(required = false) String optType,
                              @RequestParam(required = false) String orderNo,
                              @RequestParam(required = false) String type) {
        startPage();
        
        PdaWarehouseOutBillsList billsList = new PdaWarehouseOutBillsList();
        
        // 处理前端传入的参数
        if("0".equals(type)){//出库页面
            if(orderStatus.equals("all")){
                billsList.setBillsState("0,1,9");
            }else{
                billsList.setBillsState(orderStatus);
            }
        }else if("1".equals(type)){//拣货页面
            if(orderStatus.equals("all")){
                billsList.setBillsState("0,1,9");
            }else{
                billsList.setBillsState(orderStatus);
            }
        }else if("2".equals(type)){//复核页面
            if(orderStatus.equals("all")) {
                billsList.setBillsState("1,2,9");
            }else{
                billsList.setBillsState(orderStatus);
            }
        }else if("3".equals(type)){//出库确认
            if(orderStatus.equals("all")) {
                billsList.setBillsState("2,3,9");
            }else{
                billsList.setBillsState(orderStatus);
            }
        }else{
            return new TableDataInfo();
        }
        
        // optType: -1表示全部, 其他值表示具体类型
        if (optType != null && !"-1".equals(optType)) {
            billsList.setBillsType(optType);
        }
        
        // orderNo: 出库单号
        if (orderNo != null && !orderNo.isEmpty()) {
            billsList.setBillsId(orderNo);
        }
        
        List<PdaWarehouseOutBillsList> list = pdaWarehouseOutBillsService.selectPdaWarehouseOutBillsList(billsList);
        return getDataTable(list);
    }

    /**
     * 导出出库单据列表
     */
    //@PreAuthorize("@ss.hasPermi('storage:finishStore:export')")
    @Log(title = "出库单据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PdaWarehouseOutBillsList billsList) {
        List<PdaWarehouseOutBillsList> list = pdaWarehouseOutBillsService.selectPdaWarehouseOutBillsList(billsList);
        ExcelUtil<PdaWarehouseOutBillsList> util = new ExcelUtil<PdaWarehouseOutBillsList>(PdaWarehouseOutBillsList.class);
        util.exportExcel(response, list, "出库单据数据");
    }

    /**
     * 获取出库单据详细信息
     */
    //@PreAuthorize("@ss.hasPermi('storage:finishStore:query')")
    @GetMapping(value = "/{billsID}")
    public AjaxResult getInfo(@PathVariable("billsID") String billsID) {
        return success(pdaWarehouseOutBillsService.selectPdaWarehouseOutBillsByBillsID(billsID));
    }

    /**
     * 新增出库单据
     */
    //@PreAuthorize("@ss.hasPermi('storage:finishStore:add')")
    @Log(title = "出库单据", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PdaWarehouseOutBillsList billsList) {
        return toAjax(pdaWarehouseOutBillsService.insertPdaWarehouseOutBillsList(billsList));
    }

    /**
     * 修改出库单据
     */
    //@PreAuthorize("@ss.hasPermi('storage:finishStore:edit')")
    @Log(title = "出库单据", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PdaWarehouseOutBillsList billsList) {
        return toAjax(pdaWarehouseOutBillsService.updatePdaWarehouseOutBillsList(billsList));
    }

    /**
     * 删除出库单据
     */
    //@PreAuthorize("@ss.hasPermi('storage:finishStore:remove')")
    @Log(title = "出库单据", businessType = BusinessType.DELETE)
    @DeleteMapping("/{billsIDs}")
    public AjaxResult remove(@PathVariable String[] billsIDs) {
        return toAjax(pdaWarehouseOutBillsService.deletePdaWarehouseOutBillsByBillsIDs(billsIDs));
    }

    /**
     * 拣货操作
     */
    @Log(title = "出库单据-拣货", businessType = BusinessType.UPDATE)
    @PostMapping("/picking/{billsID}")
    public AjaxResult pickingGoods(@PathVariable String billsID) {
        String pickingUserId = SecurityUtils.getUsername();
        String pickingUserName = SecurityUtils.getLoginUser().getUser().getNickName();
        return toAjax(pdaWarehouseOutBillsService.pickingGoods(billsID, pickingUserId, pickingUserName));
    }

    /**
     * 复核操作
     */
    @Log(title = "出库单据-复核", businessType = BusinessType.UPDATE)
    @PostMapping("/review/{billsID}")
    public AjaxResult reviewGoods(@PathVariable String billsID) {
        String reviewUserId = SecurityUtils.getUsername();
        String reviewUserName = SecurityUtils.getLoginUser().getUser().getNickName();
        return toAjax(pdaWarehouseOutBillsService.reviewGoods(billsID, reviewUserId, reviewUserName));
    }

    /**
     * 出库操作
     */
    @Log(title = "出库单据-出库", businessType = BusinessType.UPDATE)
    @PostMapping("/deliver/{billsID}")
    public AjaxResult deliverGoods(@PathVariable String billsID) {
        String outUserId = SecurityUtils.getUsername();
        String outUserName = SecurityUtils.getLoginUser().getUser().getNickName();
        return toAjax(pdaWarehouseOutBillsService.deliverGoods(billsID, outUserId, outUserName));
    }
}

